function plusReady() {
    
    getUserInfo();
    bindEvent();
}
if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}

var _opearindex = -1;
//var jqXHR 
function getUserInfo() {

    var user = userLogin();

    //是否显示退出登录
    if (user) { $("#qucik").show(); }
    //退出登录 
    $("#qucik").on("click", function () {        
       // 清空当前用户信息
        plus.storage.removeItem(storageManager.user);
        plus.storage.removeItem(storageManager.cart);
        plus.webview.getWebviewById(pageName.mine).evalJS("receivePersonEvent(1)");
        back();
    });

    $("#user_image").attr("src", configManager.avatarImgurl.format(user.avatar_id, "150-150"));
    $("#nick").html(user.nick);
    $("#real_name").html(user.real_name);
    $("#name").html(user.name);

    var page = "";
    if ("1" == user.auth_status) {
        $("#auth_status").html("已认证");
        //page = "autherized.html";
        page = "unautherized.html";
    }
    else {
        $("#auth_status").html("未认证");
        page = "unautherized.html";
    }

    $("#real_name_link,#authentication_link").on("click", function () {
        clicked(page);
    });

    //点击上传头像
    $("#user_image").on("click", function () {
        $("#heisebg").removeClass("heisebghid").addClass("heisebg");
        //上传图片时遮罩挡住下面导航栏了
        $("#heisebg").css("z-index", "9999");
        $("#footer").css("z-index", "0");
    });

    $("#cancel").on("click", function () {
        $("#heisebg").removeClass("heisebg").addClass("heisebghid");
    })

    //从相册中选择上传
    $("#album").on("click", function () {
        plus.gallery.pick(
			function (path) {
				//选择成功
				$("#heisebg").removeClass("heisebg").addClass("heisebghid");
				$("#waitingupload").removeClass("heisebghid").addClass("heisebg");
			    var task = plus.uploader.createUpload(configManager.RequstUrl + "/api/common/upload",
					{ method: "POST", blocksize: 102400, priority: 100 },
					function (upload, status) {
					    // 上传完成
					    if (status == 200) {
					        var responseText = JSON.parse(upload.responseText);
					        var src = configManager.RequstUrl + "img/avatar-" + responseText.id + "/150-150";
					        $.ajax({
					            type: "POST",
					            url: configManager.RequstUrl + "api/user/updateavatar",
					            data: { "userid": user.id, "avatar": responseText.id, "token": user.token }
					        }).done(function (data) {
					            if ("success" == data.state) {
					                user.avatar_id = responseText.id.toString();
					                plus.storage.setItem(storageManager.user, JSON.stringify(user));
					                $("#user_image").attr("src", src);
					                $("#waitingupload").removeClass("heisebg").addClass("heisebghid");
					                _opearindex = 0;
					            }
					            else {
					                plus.nativeUI.alert(data.message);
					            }
					        });

					    } else {
					        console.log("Upload failed: " + status);
					    }
					}
				);
			    console.log(path);
			    task.addFile( path, { key: "file" });
			    task.addData("dir", "avatar");
			    task.start();
			},
			function (e) {
			    console.log(e);
			},
			{ filter: "image" }
		);
    });

    //调用摄像头拍照并上传
    $("#camera").on("click", function () {
    	
        var cmr = plus.camera.getCamera(1);
        if (null != cmr) {
            //拍照
            cmr.captureImage(function (p) {
                plus.io.resolveLocalFileSystemURL(
	        	p,
	        	function (entry) {
					//拍照成功
					$("#heisebg").removeClass("heisebg").addClass("heisebghid");
					$("#waitingupload").removeClass("heisebghid").addClass("heisebg");	        		
	        	    //上传图片
	        	    var task = plus.uploader.createUpload(configManager.RequstUrl + "/api/common/upload",
						{ method: "POST", blocksize: 102400, priority: 100 },
						function (upload, status) {
						    // 上传完成
						    if (status == 200) {
						        var responseText = JSON.parse(upload.responseText);
						        var src = configManager.RequstUrl + "img/avatar-" + responseText.id + "/150-150";
						        $.ajax({
						            type: "POST",
						            url: configManager.RequstUrl + "/api/user/updateavatar",
						            data: { "userid": user.id, "avatar": responseText.id, "token": user.token }
						        }).done(function (data) {
						            if ("success" == data.state) {
						                user.avatar_id = responseText.id.toString();
						                plus.storage.setItem(storageManager.user, JSON.stringify(user));
						                
						                $("#user_image").attr("src", src);
						                $("#waitingupload").removeClass("heisebg").addClass("heisebghid");
						                _opearindex = 0;
						            }
						            else {
						                plus.ui.alert(data.message, function () { }, configManager.alerttip, configManager.alertCtip);
						            }
						        });
						    } else {
						        console.log("Upload failed: " + status);
						    }
						}
					);
	        	    task.addFile("file://" + entry.fullPath, { key: "file" });
	        	    task.addData("dir", "avatar");
	        	    task.start();

	        	},
		        function (e) { plus.ui.alert(e.message, function () { }, configManager.alerttip, configManager.alertCtip); }
		        );
            },
		    function (e) { },
		    { filename: "_doc/camera/" });
        }
        else {
            plus.ui.alert("没有找到摄像头", function () { }, configManager.alerttip, configManager.alertCtip);
        }

    });
}

function bindEvent() {
    // 回退
    $("#back").on("click", function () {
        plus.webview.getWebviewById(pageName.mine).evalJS("receivePersonEvent(" + _opearindex + ")");
        plus.webview.getWebviewById(pageName.main).evalJS("gohome()");
    });
}


